gRPC (Google Remote Procedure Call) একটি হাই-পারফরম্যান্স RPC ফ্রেমওয়ার্ক যা Protocol Buffers (protobuf) ব্যবহার করে। Spring Boot-এর মাধ্যমে gRPC সার্ভিসগুলোর সাথে ক্লায়েন্ট ইন্টিগ্রেশন সহজেই করা যায়।
gRPC Client Integration এর ধাপসমূহ:
১. প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করা
Maven ব্যবহার করলে pom.xml এ প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করুন।
<dependencies>
<!-- gRPC -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.57.2</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.57.2</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.57.2</version>
</dependency>
<!-- Protocol Buffers -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.23.4</version>
</dependency>
</dependencies>
Protobuf প্লাগইন:
Maven এর জন্য Protobuf প্লাগইন যোগ করুন:
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.23.4:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.57.2:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
২. Proto ফাইল তৈরি করা
src/main/proto/ ডিরেক্টরির মধ্যে .proto ফাইল তৈরি করুন।
example.proto:
syntax = "proto3";
package com.example.grpc;
service GreetingService {
rpc greet (GreetingRequest) returns (GreetingResponse);
}
message GreetingRequest {
string name = 1;
}
message GreetingResponse {
string message = 1;
}
৩. Proto ফাইল কম্পাইল করা
Maven বিল্ড চালান:
mvn clean compile
এটি Java স্টাব ফাইল জেনারেট করবে: target/generated-sources/protobuf
৪. gRPC Client Configuration
Spring Boot-এ gRPC ক্লায়েন্ট কনফিগার করতে একটি Bean তৈরি করুন।
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GrpcClientConfig {
@Bean
public ManagedChannel managedChannel() {
return ManagedChannelBuilder.forAddress("localhost", 9090) // gRPC server address
.usePlaintext() // For development; disable SSL/TLS
.build();
}
}
৫. gRPC Client ব্যবহার
gRPC ক্লায়েন্ট স্টাব ব্যবহার করে সার্ভারে RPC কল করুন।
import com.example.grpc.GreetingRequest;
import com.example.grpc.GreetingResponse;
import com.example.grpc.GreetingServiceGrpc;
import io.grpc.ManagedChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GrpcClientService {
private final GreetingServiceGrpc.GreetingServiceBlockingStub blockingStub;
@Autowired
public GrpcClientService(ManagedChannel managedChannel) {
this.blockingStub = GreetingServiceGrpc.newBlockingStub(managedChannel);
}
public String sendGreeting(String name) {
GreetingRequest request = GreetingRequest.newBuilder()
.setName(name)
.build();
GreetingResponse response = blockingStub.greet(request);
return response.getMessage();
}
}
৬. Spring Boot Controller তৈরি
gRPC ক্লায়েন্ট কল করতে একটি REST এন্ডপয়েন্ট তৈরি করুন।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GrpcClientController {
@Autowired
private GrpcClientService grpcClientService;
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return grpcClientService.sendGreeting(name);
}
}
৭. gRPC সার্ভার তৈরি (ঐচ্ছিক)
আপনার ক্লায়েন্টের জন্য একটি gRPC সার্ভার প্রয়োজন হতে পারে। উদাহরণস্বরূপ:
import com.example.grpc.GreetingRequest;
import com.example.grpc.GreetingResponse;
import com.example.grpc.GreetingServiceGrpc;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
@GRpcService
public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
@Override
public void greet(GreetingRequest request, StreamObserver<GreetingResponse> responseObserver) {
String message = "Hello, " + request.getName();
GreetingResponse response = GreetingResponse.newBuilder()
.setMessage(message)
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
Spring Boot Application চালানো
- gRPC সার্ভার চালু করুন।
- Spring Boot ক্লায়েন্ট চালু করুন।
ব্রাউজার বা Postman ব্যবহার করে REST API কল করুন:
http://localhost:8080/greet?name=John
Response:
"Hello, John"
gRPC Client Integration এর সুবিধা
- হাই-পারফরম্যান্স: HTTP/2 ব্যবহার করে।
- কোড জেনারেশন: Proto ফাইল থেকে স্টাব জেনারেট করা হয়।
- কমপ্যাক্ট ডেটা ফরম্যাট: Protocol Buffers ডেটা সেরিয়ালাইজেশনে দক্ষ।
- বহুভাষার সমর্থন: gRPC বিভিন্ন ভাষায় সাপোর্ট করে।
উপসংহার
Spring Boot এর মাধ্যমে gRPC ক্লায়েন্ট তৈরি করা খুবই সহজ এবং এটি উচ্চ-পারফরম্যান্স ডিস্ট্রিবিউটেড সিস্টেমের জন্য আদর্শ। এটি REST-এর তুলনায় দ্রুত এবং দক্ষ সমাধান প্রদান করে।
Read more